package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.TbArticle;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.jeecg.modules.system.service.ITbArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 文章内容
 * @Author: yfkj
 * @Date:   2023-02-03
 * @Version: V1.0
 */
@Api(tags="文章内容")
@RestController
@RequestMapping("/sys/tbArticle")
@Slf4j
public class TbArticleController extends JeecgController<TbArticle, ITbArticleService> {

	@Autowired
	private ITbArticleService tbArticleService;
	@Autowired
	private ISysCategoryService iSysCategoryService;


	/**
	 * 分页列表查询
	 *
	 * @param tbArticle
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "文章内容-分页列表查询")
	@ApiOperation(value="文章内容-分页列表查询", notes="文章内容-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<TbArticle>> queryPageList(TbArticle tbArticle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TbArticle> queryWrapper = QueryGenerator.initQueryWrapper(tbArticle, req.getParameterMap());
		Page<TbArticle> page = new Page<TbArticle>(pageNo, pageSize);
		IPage<TbArticle> pageList = tbArticleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param tbArticle
	 * @return
	 */
	@AutoLog(value = "文章内容-添加")
	@ApiOperation(value="文章内容-添加", notes="文章内容-添加")
	//@RequiresPermissions("tbArticle:tb_article:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TbArticle tbArticle) {
		//分类字典
		SysCategory category = iSysCategoryService.getById(tbArticle.getCataId());
		if (oConvertUtils.isEmpty(category)) return Result.error("文章分类不能为空");
		tbArticle.setCataCode(category.getCode());
		tbArticleService.save(tbArticle);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tbArticle
	 * @return
	 */
	@AutoLog(value = "文章内容-编辑", dataType = 2)
	@ApiOperation(value="文章内容-编辑", notes="文章内容-编辑")
	//@RequiresPermissions("tbArticle:tb_article:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TbArticle tbArticle) {
		//分类字典
		SysCategory category = iSysCategoryService.getById(tbArticle.getCataId());
		if (oConvertUtils.isEmpty(category)) return Result.error("文章分类不能为空");
		tbArticle.setCataCode(category.getCode());
		tbArticleService.updateById(tbArticle);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "文章内容-通过id删除")
	@ApiOperation(value="文章内容-通过id删除", notes="文章内容-通过id删除")
	//@RequiresPermissions("tbArticle:tb_article:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		tbArticleService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "文章内容-批量删除")
	@ApiOperation(value="文章内容-批量删除", notes="文章内容-批量删除")
	//@RequiresPermissions("tbArticle:tb_article:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tbArticleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "文章内容-通过id查询")
	@ApiOperation(value="文章内容-通过id查询", notes="文章内容-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TbArticle> queryById(@RequestParam(name="id",required=true) String id) {
		TbArticle tbArticle = tbArticleService.getById(id);
		if(tbArticle==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tbArticle);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tbArticle
    */
    //@RequiresPermissions("tbArticle:tb_article:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbArticle tbArticle) {
        return super.exportXls(request, tbArticle, TbArticle.class, "文章内容");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("tbArticle:tb_article:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbArticle.class);
    }

	 /**
	  * 发布、撤销文章
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "文章内容-发布、撤销文章")
	 @ApiOperation(value="文章内容-发布、撤销文章", notes="文章内容-发布、撤销文章")
	 @GetMapping(value = "/statusOne")
	 public Result<TbArticle> statusOne(@RequestParam(name="id",required=true) String id,
										  @RequestParam(name="status",required=true) int status) {
		 UpdateWrapper<TbArticle> updateWrapper = new UpdateWrapper<TbArticle>();
		 updateWrapper.lambda().set(TbArticle::getStatus,status);
		 if(status == 1){
			 updateWrapper.lambda().set(TbArticle::getReleaseBy, TokenUtils.getLoginUser().getUsername());
			 updateWrapper.lambda().set(TbArticle::getReleaseTime,new Date());
		 }
		 updateWrapper.lambda().eq(TbArticle::getId,id);
		 tbArticleService.update(new TbArticle(),updateWrapper);
		 return Result.OK("操作成功!");
	 }

	/**
	 * 根据分类编码查询文章内容
	 * @param code
	 * @return
	 */
	@GetMapping(value = "/queryArticleByCode")
	public String queryArticleByCode(@RequestParam(name="code",required=true) String code) {
		TbArticle article = tbArticleService.getOne(new LambdaQueryWrapper<TbArticle>()
				.select(TbArticle::getContent).eq(TbArticle::getCataCode,code)
				.orderByDesc(TbArticle::getCreateTime).last("limit 0,1"));
		if (oConvertUtils.isEmpty(article)) return null;
		return article.getContent();
	}

}
